import storage from '@/utils/storage'

var utils = {};

//公共处理方法
utils.install = function(Vue) {
	// 跳转
	Vue.prototype.jumpTo = (url, data) => {
		url += (url.indexOf('?') < 0 ? '?' : '&') + Vue.prototype.param(data)
		uni.navigateTo({
			url
		})
	};
	// 参数跳转验证
	Vue.prototype.param = (data) => {
		let url = ''
		for (var k in data) {
			let value = data[k] !== undefined ? data[k] : ''
			url += '&' + k + '=' + encodeURIComponent(value)
		}
		return url ? url.substring(1) : ''
	};
	// 返回
	Vue.prototype.goBack = (num = -1) => {
		uni.navigateBack({
			delta: num
		});
	};
	/**
	 * 字符串超出省略号
	 * @param {String} value 字符串
	 * @param {Number} len 长度
	 */
	Vue.prototype.hideText = (value, len) => {
		if (!value) return ''
		if (value.length > len) {
			return value.slice(0, len) + '...'
		}
		return value
	};
	//去除对象的空属性
	Vue.prototype.removePropertyOfNull = (obj) => {
		Object.keys(obj).forEach(item => {
			if (obj[item] === null) delete obj[item]
		})
		return obj;
	};
	//uuid
	Vue.prototype.getUuid = (len, radix) => {
		const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
		const uuid = []
		radix = radix || chars.length

		if (len) {
			// Compact form
			for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
		} else {
			// rfc4122, version 4 form
			let r

			// rfc4122 requires these characters
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
			uuid[14] = '4'

			// Fill in random data.  At i==19 set the high bits of clock sequence as
			// per rfc4122, sec. 4.1.5
			for (let i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | Math.random() * 16
					uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r]
				}
			}
		}
		return uuid.join('') + new Date().getTime()
	};
	/**
	 * 检查是否有权限
	 * 使用方法 v-if="checkHasPermission('baseUnit:list')"
	 * @param {String} permissions 权限代码
	 */
	Vue.prototype.checkHasPermission = (permissions) => {
		// const user = uni.getStorageSync('user');
		const authorities = storage.get('authorities');
		let isDisplay = true
		// if (user && user.authorities) {
		if (authorities && authorities.length > 0) {
			// 登录信息中保存的权限
			// const authorities = user.authorities.map(item => item.authority);
			// const authoritiesArr = authorities.map(item => item.authority);
			// for (const v of authoritiesArr) {
			// 	// if (!authorities.includes(v)) {
			// 	if (!authoritiesArr.includes(permissions)) {
			// 		isDisplay = false
			// 	}
			// }
			if (!authorities.includes(permissions)) {
				isDisplay = false
			} else {
			isDisplay = true
			}
		} 
		return isDisplay
	};
	// 
	/**
	 * 根据数组对象属性删除对应对象
	 * @param {Array} arr 数组对象
	 * @param {String} attr 对应属性
	 * @param {String} value 对应属性值
	 */
	Vue.prototype.removeByValue = (arr, attr, value) => {
		var index = 0;
		for (var i in arr) {
			if (arr[i][attr] == value) {
				index = i;
				break;
			}
		}
		arr.splice(index, 1);
	};
	// 时间戳转几小时几分钟
	Vue.prototype.timestampToTime = (timestamp)=> {
		if (!timestamp) return 0
		const date = new Date(timestamp * 1000) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let Y = `${date.getFullYear()}year`
		let M = `${date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}month` : date.getMonth() + 1}month`
		let D = `${date.getDate()}day`
		const h = `${date.getHours()}h`
		const m = `${date.getMinutes()}min`
		// s = date.getSeconds()
		return h + m
	};
};

export default utils;
